package com.du.web.websocket.model;

import lombok.Data;

import java.io.Serializable;

/**
 * @desc:
 * @author: shuangdu
 * @date: 2021/8/5
 * @since: 1.8
 */
@Data
public class ResBean<T> implements Serializable {
    // 成功
    public static final int SUCCESS = 0;
    // 失败
    public static final int FAIL = 1;

    public static final String DEFAULT_MSG = "操作成功";

    public static final String DEFAULT_ERROR_MSG = "操作失败";

    private String message = "success";

    private int code = SUCCESS;

    private T data;

    private boolean success;


    public ResBean() {
        super();
        this.success = true;
    }

    public ResBean(T data) {
        super();
        this.data = data;
    }

    public ResBean(String message) {
        super();
        this.message = message;
    }

    public ResBean(int code, String message) {
        super();
        this.code = code;
        this.message = message;
    }

    public ResBean(int code, String message, T data) {
        super();
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public ResBean(T data, String message) {
        super();
        this.data = data;
        this.message = message;
    }



    public ResBean(Throwable e) {
        super();
        this.message = e.getMessage();
        this.code = FAIL;
    }

    public ResBean(Throwable e, int code) {
        super();
        this.message = e.getMessage();
        this.code = code;
    }

    public static ResBean<?> ok() {
        ResBean<?> res = new ResBean<>();
        res.setCode(SUCCESS);
        res.setMessage(DEFAULT_MSG);
        return res;
    }
    public static ResBean<?> ok(String message) {
        ResBean<?> res = ResBean.ok();
        res.setMessage(message);
        return res;
    }
    public static <T> ResBean<T> ok(T data) {
        ResBean<T> res = new ResBean<>();
        res.setCode(SUCCESS);
        res.setMessage("success");
        res.setData(data);
        return res;
    }

    public static ResBean<?> error() {
        ResBean<?> res = new ResBean<>();
        res.setCode(FAIL);
        res.setMessage(DEFAULT_ERROR_MSG);
        res.setSuccess(Boolean.FALSE);
        return res;
    }
    public static ResBean<?> error(String message) {
        ResBean<?> res = ResBean.error();
        res.setMessage(message);
        return res;
    }
}
